# 图像顶帽运算(原始图像 — 开运算)

import cv2
import numpy as np

# 读取图片
src = cv2.imread('src/img_2.png', cv2.IMREAD_UNCHANGED)

# 设置卷积核
kernel = np.ones((5, 5), np.uint8)

# 图像闭运算(卷积核5x5)
result = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", result)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

# 设置卷积核
kernel = np.ones((15, 15), np.uint8)

# 图像闭运算(卷积核15x15)
result = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)
# 显示图像
cv2.imshow("src", src)
cv2.imshow("result", result)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()
